package com.jye.rseven.user.controller;



import com.jye.rseven.Constants.MyConstants;
import com.jye.rseven.domain.Activities;
import com.jye.rseven.domain.Task;
import com.jye.rseven.service.ActivitiesService;
import com.jye.rseven.service.TaskService;
import com.jye.rseven.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activities")
@Api(tags = "活动控制器API")
public class ActivitiesController {

    @Autowired
    ActivitiesService activitiesService;

    @Autowired
    TaskService taskService;


    @GetMapping("/findAllActivities")
    @ApiOperation(value = "查找所有活动", notes = "根据限制查找所有活动", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findAllActivities(int skip,int limit){
        JsonResult jsonResult = new JsonResult();
        List<Activities> activitiesList = activitiesService.findAllActivities(skip,limit);
        if(activitiesList.size()!=0){
            jsonResult.setData(activitiesList);
        }else {
            jsonResult.setMsg("查询失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @GetMapping("/findActivitiesByIdAndUId")
    @ApiOperation(value = "查找活动", notes = "根据活动id和用户id查找活动", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "活动id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findActivitiesByIdAndUId(int id,int uId){
        JsonResult jsonResult = new JsonResult();
        Activities activities = activitiesService.findActivitiesByIdAndUId(id,uId);
        if(!StringUtils.isEmpty(activities)){
            jsonResult.setData(activities);
        }else {
            jsonResult.setMsg("查询失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @GetMapping("/findMyActivities")
    @ApiOperation(value = "查找我的活动", notes = "根据用户uId查找我的活动", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer")
    })
    public JsonResult findMyActivities(int uId,int skip,int limit){
        JsonResult jsonResult = new JsonResult();
        List<Activities> activitiesList = activitiesService.findMyActivities(uId,skip,limit);
        if(activitiesList.size()!=0){
            jsonResult.setData(activitiesList);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("你暂无课程!");
        }
        return jsonResult;
    }

    @GetMapping("/findActivitiesByTitle")
    @ApiOperation(value = "模糊查找活动", notes = "根据名字模糊查找活动", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "title",
                    value = "活动名字", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条",dataType = "Integer")
    })
    public JsonResult findActivitiesByTitle(String title, @RequestParam(required = false) Integer skip, @RequestParam(required = false)Integer limit){
        JsonResult jsonResult = new JsonResult();
        if(skip==null || limit == null){
            skip = 0;
            limit = 30;
        }
        List<Activities> activitiesList = activitiesService.findActivitiesByTitle(title,skip,limit);
        if(activitiesList.size()!=0){
            jsonResult.setData(activitiesList);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("抱歉!暂无查询到["+title+"]的活动!");
        }
        return jsonResult;
    }

    @GetMapping("/findMyActivitesByUIdAndType")
    @ApiOperation(value = "查找我的类别活动", notes = "根据用户uId活动类别查找活动", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户id", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "type",
                    value = "活动类别", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer")
    })
    public JsonResult findMyActivitesByUIdAndType(int uId,String type,int skip,int limit){
        JsonResult jsonResult = new JsonResult();
        List<Activities> activitiesList = activitiesService.findMyActivitesByUIdAndType(uId,type,skip,limit);
        if(activitiesList.size()!=0){
            jsonResult.setData(activitiesList);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("抱歉!暂无课程查询到["+type+"]类别的课程!");
        }
        return jsonResult;
    }


    @GetMapping("/findActivitesByType")
    @ApiOperation(value = "查找不同类别活动", notes = "根据活动类别查找活动", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "type",
                    value = "活动类别", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer")
    })
    public JsonResult findActivitesByType(String type,int skip,int limit){
        JsonResult jsonResult = new JsonResult();
        List<Activities> activitiesList = activitiesService.findActivitesByType(type,skip,limit);
        if(activitiesList.size()!=0){
            jsonResult.setData(activitiesList);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("抱歉!暂无课程查询到["+type+"]类别的课程!");
        }
        return jsonResult;
    }


    @PostMapping("/addActivities")
    @ApiOperation(value = "创建活动", notes = "根据标题title,描述desc,规则rules,封面imgUrl,用户uId,类别type创建活动", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "activities",
                    value = "活动实体类", required = true),
    })
    public JsonResult addActivities(Activities activities){
        JsonResult jsonResult = new JsonResult();
        String msg = activitiesService.addActivities(activities);
        jsonResult.setMsg(msg);
        return jsonResult;
    }


    @PutMapping("/closeActivitiesById")
    @ApiOperation(value = "冻结活动", notes = "根据id冻结活动", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "活动id", required = true),
    })
    public JsonResult closeActivitiesById(int id){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(activitiesService.closeActivitiesById(id));
        return jsonResult;
    }

    @PutMapping("/updateActivities")
    @ApiOperation(value = "修改活动信息", notes = "根据id修改活动信息(只能修改描述desc,封面imgUrl,规则rules,类别type)", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "活动类", required = true),
    })
    public JsonResult updateActivities(Activities activities){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(activitiesService.updateActivities(activities));
        return jsonResult;
    }

    @DeleteMapping("/deleteActivitiesById")
    @ApiOperation(value = "删除活动", notes = "根据id删除活动", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id",
                    value = "活动id", required = true),
    })
    public JsonResult deleteActivitiesById(int id){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(activitiesService.deleteActivitiesById(id));
        return jsonResult;
    }


//    @GetMapping("/findAcitivitiesByTitle")
//    @ApiOperation(value = "模糊查找活动", notes = "根据title模糊查找活动", httpMethod = "GET")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", name = "title",
//                    value = "模糊查询关键字", required = true,
//                    dataType = "Integer"),
//    })
//    public JsonResult findAcitivitiesByTitle(String title){
//        JsonResult jsonResult = new JsonResult();
//        List<Activities> activitiesList = activitiesService.findAcitivitiesByTitle(title);
//        if(activitiesList.size()!=0){
//            jsonResult.setData(activitiesList);
//        }else{
//            jsonResult.setCode("500");
//            jsonResult.setMsg("赞无关键字为"+title+"的活动!");
//        }
//        return jsonResult;
//    }




}
